package com.cocos.game

import android.R.attr.value
import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import android.util.Log
import com.cocos.service.SDKWrapper.SDKInterface
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


class MiscSDK : SDKInterface {

    companion object {
        @SuppressLint("StaticFieldLeak")
        var instance: MiscSDK? = null
        private const val TAG = "MiscSDK"
    }

    var activity: Activity? = null

    override fun init(context: Context?) {
        this.activity = context as Activity
        Log.i(TAG, "init: $TAG")
        instance = this

        JsbSDK.onMsg("MiscSDK.requestInfoDictionary") { args -> this.getInfoDictionary(args) }
        JsbSDK.onMsg("MiscSDK.requestDeviceInfo") { args -> this.getDeviceInfo(args) }
        JsbSDK.onMsg("MiscSDK.requestHaptic") { args -> this.haptic(args) }
    }

    private fun getInfoDictionary(args: String) {
        val packageManager = this.activity?.packageManager
        val packageInfo = packageManager?.getPackageInfo(this.activity?.packageName.toString(), 0);
        Log.i(TAG, "getInfoDictionary: ${packageInfo?.versionName} ${packageInfo?.versionCode}");
        val infoDictionary = mapOf(
            "versionName" to packageInfo?.versionName,
            "versionCode" to packageInfo?.versionCode,
        )
        JsbSDK.sendMsg("MiscSDKManager.responseInfoDictionary", infoDictionary);
    }

    private fun getDeviceInfo(args: String) {
        val deviceInfoDictionary = mapOf(
            "manufacturer" to Build.MANUFACTURER,
            "brand" to Build.BRAND,
            "model" to Build.MODEL,
        )
        JsbSDK.sendMsg("MiscSDKManager.responseDeviceInfo", deviceInfoDictionary);
    }

    @SuppressLint("ObsoleteSdkInt", "UseRequiresApi")
    @TargetApi(Build.VERSION_CODES.S)
    fun haptic(args: String) {
        Log.i(TAG, "haptic: $args");
        val argMap: Map<String, Any> = Gson().fromJson(args, object : TypeToken<Map<String, Any>>() {}.type);
        val duration = argMap["duration"] as? Number
        val style = argMap["amp"]
        val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            (activity?.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager).defaultVibrator
        } else {
            activity?.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val vibrationEffect = when (style) {
                "tick" -> VibrationEffect.createOneShot(duration?.toLong()?:0, VibrationEffect.EFFECT_TICK)
                "click" -> VibrationEffect.createOneShot(duration?.toLong()?:0, VibrationEffect.EFFECT_CLICK)
                "heavy-click" -> VibrationEffect.createOneShot(duration?.toLong()?:0, VibrationEffect.EFFECT_HEAVY_CLICK)
                else -> VibrationEffect.createOneShot(duration?.toLong()?:0, VibrationEffect.DEFAULT_AMPLITUDE)
            }
            vibrator.vibrate(vibrationEffect)
        } else {
            vibrator.vibrate(duration?.toLong()?:0)
        }
    }
}